$(function(){
	$('#main_menu li a').click(function(e){
		setMenuItemActive($(this).parent("li").attr("id"));
	});
	
	$('.sidebar').hover(function(){
		openSidebar();
	}, function(){
		closeSidebar();
	});

	$(".navbar-brand").click(toggleSidebar);
});

function setMenuItemActive(menuItem){
	$('#main_menu li').removeClass("active");
	$('#'+menuItem).addClass("active");
}

function toggleSidebar() {
	if($('.sidebar').hasClass("open"))
		closeSidebar();
	else
		openSidebar();
}

function openSidebar() {
	$('.sidebar').addClass("open");
	$(".navbar-brand i").removeClass("fa-indent").addClass("fa-outdent");
	$('.sidebar').stop().animate({
		left: 0
	}, 800);
}

function closeSidebar() {
	$('.sidebar').removeClass("open");
	$(".navbar-brand i").removeClass("fa-outdent").addClass("fa-indent");
	$('.sidebar').stop().animate({
		left: -140
	}, 800);
}

function showNoty(errorType, errorMessage) {
	var n = noty({
		text: errorMessage,
		type: errorType
	});
}

function readServerError(errorCode) {
	var message = null,
        errorType = null;
    switch(errorCode) {
        case 440:
            errorType = "error";
            message = "Your session has expired. Please log in again";
            break;
        default:
            errorType = "error";
            message = null;
            break;
    }
    return {
    	"message": message,
    	"errorType": errorType
    };
}